// settings

var timerInterval = 250; // update timer in millisec
var fadingAt = 10; // fading before end in seconds
var sliderInterval = 10; // update slider timer in ms

// vars

// main vid var
var vid = new Array();
// timer var
var timer;
// is slot cueing?
var vidCueing = new Array();
// slot state
var vidState = new Array();
// slot volume
var vidVolume = new Array();
// fader pos
var faderPos;
// current slot
var currentSlot = 0;

// BUG: drop search result in playlist may add 1-3 entries in playlist
var preventDrop = false;

// set initial vars

function init() {
	
	printLog('Players ready...');
	
	// start timer
	timer = setInterval("timerEvent()", timerInterval);
		
	//sliders
	sliderInit(0);
	sliderInit(1);
	
	// volume sliders
	volumeInit(0);
	volumeInit(1);
	
	//fader
	faderInit();	
	
	// set current slot
	setActiveSlot(currentSlot);
	
	// set droppable playlist
	$("#playlist-list").droppable({ 
	    accept: ".search-result", 
	    hoverClass: "playlist-dropnow",
	    activeClass: "playlist-dropactive",
	    tolerance: "touch",
	    drop: function(ev, ui) {
	    	if (!preventDrop) {
		    	vidid = $(ui.draggable).attr("id").replace('search_','');
		    	if (!playlistVidExists(vidid)) {
		    		loadPlaylistEntry(vidid);
		    	}
		    	// prevent adding more songs to the playlist within 1 second
		    	preventDrop = true;
		    	setTimeout(function() { preventDrop = false; }, 1000);
	    	}	    	
	    } 
	});
	
	// set droppable video overlay
	$("div.vid-ytoverlay").droppable({ 
	    accept: ".result", 
	    hoverClass: "vid-ytoverlay-dropnow", 
	    activeClass: "vid-ytoverlay-dropactive",
	    tolerance: "intersect",
	    drop: function(ev, ui) {
	    	if (!preventDrop) {
		    	vidid = $(ui.draggable).attr("id").replace('search_','');
		    	vidid = vidid.replace('entry_','');
		    	slot = $(this).attr("id").replace('vid-','').replace('-ytoverlay','');
		    	videoCue(vidid, slot, 0);
		    	if ($(ui.draggable).hasClass('search-result')) {
		    	} else {
		    		playlistRemoveEntry(vidid);
		    	}
		    	// prevent adding more songs to the video overlay within 1 second
		    	preventDrop = true;
		    	setTimeout(function() { preventDrop = false; }, 1000);
	    	}	    	
	    } 
	});
	
	// ie: set opacity of video overlay
	//$("div.vid-ytoverlay").css("opacity", 0.5);
	
}

// dom ready

$(document).ready(function(){

	// init swf
	var yourDevKey = 'AI39si4v-2HnjtqSrwpoDrn5xqpPhwDUfmut_XkJU6qddWa_JVdXSuOPqqfnmczS9hDWg2NxLh6k0H8Ha9kMBggLf-6dutR5_w';
	var params = { allowScriptAccess: "always", bgcolor: "#cccccc", wmode: "transparent" };
	var atts0 = { id: "vid0player", wmode: "transparent" };
	var atts1 = { id: "vid1player", wmode: "transparent" };
	
	// load swf
	swfobject.embedSWF("http://gdata.youtube.com/apiplayer?key=" + yourDevKey + "&enablejsapi=1&playerapiid=vid0", "vid-0", "425", "356", "8", null, null, params, atts0);
	swfobject.embedSWF("http://gdata.youtube.com/apiplayer?key=" + yourDevKey + "&enablejsapi=1&playerapiid=vid1", "vid-1", "425", "356", "8", null, null, params, atts1);
	
});

// player ready

var playersReady = 0;
function onYouTubePlayerReady(playerId) {
	if (playerId=='vid0') {
		vid[0] = document.getElementById("vid0player");
		vid[0].addEventListener("onStateChange", "onVid0StateChange");
	} else {
		vid[1] = document.getElementById("vid1player");
		vid[1].addEventListener("onStateChange", "onVid1StateChange");		
	}
	playersReady++;
	if (playersReady>1) { init(); }
}